package tool

import (
	"fmt"
	"gin_wuwen_back/model"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

var Dbengin *Orm

type Orm struct {
	*xorm.Engine
}

func OrmEngine(cfg *Config_Json) (*Orm, error) {

	database := cfg.Database

	conn := database.User + ":" + database.Password + "@tcp(" + database.Host + ":" + database.Port + ")/" + database.DbName + "?charset=" + database.Charset
	engine, err := xorm.NewEngine(database.Driver, conn)
	if err != nil {

		fmt.Println(err.Error())
		return nil, err
	}
	engine.ShowSQL(true) //打印sql语句
	fmt.Println("1")
	errss := engine.Sync2(new(model.SmsCode), new(model.Member), new(model.FoodCategory), new(model.Shop), new(model.Service), new(model.ShopService), new(model.VideoUrl)) //创建表在这里进行创建

	if errss != nil {
		fmt.Println(database)
		fmt.Println(errss.Error())
		return nil, err
	}

	orm := new(Orm) //记住这样定义只是函数内的变量，无法全局调用
	orm.Engine = engine
	Dbengin = orm

	//初始化一部分数据
	InsertShope()
	InitVideoUrl()
	return orm, nil

}

//我们在这做一些数据的提前准备
func InsertShope() {
	shops := []model.Shop{
		model.Shop{Id: 1, Name: "嘉禾一品（温都水城）", Address: "北京市昌平区宏福苑温都水城F1", Longitude: 116.36868, Latitude: 40.10039,
			Phone: "13437850035", Status: 1, RecentOrderNum: 106, RatingCount: 961, Rating: 4.7, PromotionInfo: "欢迎光临，用餐高峰请提前下单，谢谢",
			OpeningHours: "8:30/20:30", IsNew: true, IsPremium: true, ImagePath: "", MinimumOrderAmount: 20, DeliveryFee: 5},
		model.Shop{Id: 479, Name: "杨国福麻辣烫", Address: "北京市市蜀山区南二环路天鹅湖万达广场8号楼1705室", Longitude: 117.22124, Latitude: 31.81948, Phone: "13167583411",
			Status: 1, RecentOrderNum: 755, RatingCount: 167, Rating: 4.2, PromotionInfo: "欢迎光临，用餐高峰请提前下单，谢谢", OpeningHours: "8:30/20:30",
			IsNew: true, IsPremium: true, ImagePath: "", MinimumOrderAmount: 20, DeliveryFee: 5},
		model.Shop{Id: 485, Name: "好适口", Address: "北京市海淀区西二旗大街58号", Longitude: 120.65355, Latitude: 31.26578, Phone: "12345678901",
			Status: 1, RecentOrderNum: 58, RatingCount: 576, Rating: 4.6, PromotionInfo: "欢迎光临，用餐高峰请提前下单，谢谢", OpeningHours: "8:30/20:30",
			IsNew: true, IsPremium: true, ImagePath: "", MinimumOrderAmount: 20, DeliveryFee: 5},
		model.Shop{Id: 486, Name: "东来顺旗舰店", Address: "北京市天河区东圃镇汇彩路38号1领汇创展商务中心401", Longitude: 113.41724, Latitude: 23.1127, Status: 1,
			Phone: "13544323775", RecentOrderNum: 542, RatingCount: 372, Rating: 4.2, PromotionInfo: "老北京正宗涮羊肉,非物质文化遗产",
			OpeningHours: "09:00/21:30", IsNew: true, IsPremium: true, ImagePath: "", MinimumOrderAmount: 20, DeliveryFee: 5},
		model.Shop{Id: 487, Name: "北京酒家", Address: "北京市海淀区上下九商业步行街内", Longitude: 113.24826, Latitude: 23.11488, Phone: "13257482341", Status: 0,
			RecentOrderNum: 923, RatingCount: 871, Rating: 4.2, PromotionInfo: "北京第一家传承300年酒家", OpeningHours: "8:30/20:30", IsNew: true, IsPremium: true, ImagePath: "",
			MinimumOrderAmount: 20, DeliveryFee: 5},
		model.Shop{Id: 488, Name: "和平鸽饺子馆", Address: "北京市越秀区德政中路171", Longitude: 113.27521, Latitude: 23.12092,
			Phone: "17098764762", Status: 1, RecentOrderNum: 483, RatingCount: 273, Rating: 4.2, PromotionInfo: "吃饺子就来和平鸽饺子馆", OpeningHours: "8:30/20:30",
			IsNew: true, IsPremium: true, ImagePath: "", MinimumOrderAmount: 20, DeliveryFee: 5}}

	session := Dbengin.NewSession()
	defer session.Close()
	err := session.Begin()
	for _, shop := range shops {
		_, err := session.Insert(&shop)
		if err != nil {
			session.Rollback()
			return
		}
	}
	err = session.Commit()
	if err != nil {
		fmt.Println(err.Error())
	}

}

//在这也做一些数据准备
func InitVideoUrl() {
	Urls := []model.VideoUrl{
		model.VideoUrl{Id: 1, Vname: "在", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/57ffa91a5285890818698012759/v.f100030.mp4"},
		model.VideoUrl{Id: 2, Vname: "终于明白", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b976d4e5285890818700297037/v.f100030.mp4"},
		model.VideoUrl{Id: 3, Vname: "只要", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/4369783a5285890818565507874/v.f100030.mp4"},
		model.VideoUrl{Id: 4, Vname: "一个", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/436977985285890818565507827/v.f100030.mp4"},
		model.VideoUrl{Id: 5, Vname: "我", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/6af2f49b5285890818698855700/v.f100030.mp4"},
		model.VideoUrl{Id: 6, Vname: "天真", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b73cca55285890818700278110/v.f100030.mp4"},
		model.VideoUrl{Id: 7, Vname: "十年之后", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b7345095285890818700277250/v.f100030.mp4"},
		model.VideoUrl{Id: 8, Vname: "哭", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b72c5105285890818700276528/v.f100030.mp4"},
		model.VideoUrl{Id: 9, Vname: "开始", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b72b0b25285890818700276098/v.f100030.mp4"},
		model.VideoUrl{Id: 10, Vname: "孩子", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b7238f05285890818700275547/v.f100030.mp4"},
		model.VideoUrl{Id: 11, Vname: "还是", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/1b71b5735285890818700274776/v.f100030.mp4"},
		model.VideoUrl{Id: 12, Vname: "中", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/eb1cd8ee3701925922463946296/v.f100030.mp4"},
		model.VideoUrl{Id: 13, Vname: "北", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/3c01a0c03701925922342029843/v.f100030.mp4"},
		model.VideoUrl{Id: 14, Vname: "大学", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/eb1d60653701925922463947142/v.f100030.mp4"},
		model.VideoUrl{Id: 15, Vname: "冲击", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/eb1d6dcd3701925922463947492/v.f100030.mp4"},
		model.VideoUrl{Id: 16, Vname: "双", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/23157b053701925922464040958/v.f100030.mp4"},
		model.VideoUrl{Id: 17, Vname: "一", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/3c01a5003701925922342029942/v.f100030.mp4"},
		model.VideoUrl{Id: 18, Vname: "流", Url: "http://1304563715.vod2.myqcloud.com/9a8990advodtranscq1304563715/934357863701925922448911293/v.f100030.mp4"},
	}
	session := Dbengin.NewSession()
	defer session.Close()
	err := session.Begin()
	for _, url := range Urls {
		_, err := session.Insert(&url)
		if err != nil {
			session.Rollback()
			return
		}
	}
	err = session.Commit()
	if err != nil {
		fmt.Println(err.Error())
	}

}
